//
//  MIDIClubViewCtrler.m
//  MIDI
//
//  Created by 千行时线 on 15/9/14.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "MIDIClubViewCtrler.h"
#import "MIDIClubCell.h"
#import "UIViewController+NavItem.h"
#import "UIViewController+Create.h"
#import "AppConfig.h"
#import "APIAdress.h"
#import "QXNetWorkTool.h"
#import "MidiClubDetailModel.h"
#import "MBProgressHUD+YC.h"
#import "UIImageView+WebCache.h"
#import "UIActionSheet+Blocks.h"

@interface MIDIClubViewCtrler ()<UITableViewDataSource,UITableViewDelegate , QXNetWorkToolDelegate , UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (strong , nonatomic) MidiClubDetailModel *detailModel;
@property (strong , nonatomic) MidiClubWebViewModel *webViewModel;
@property (strong , nonatomic) QXNetWorkTool *netTool;
@property (copy , nonatomic) NSString * exam_site_id;

@end

@implementation MIDIClubViewCtrler

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.webView.backgroundColor = [UIColor clearColor];
    
    self.webView.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.webView.layer.borderWidth = 0;
    
    self.netTool = [[QXNetWorkTool alloc]init];
    self.netTool.QXNetWorkDelegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
    if(self.detailModel == nil
       && self.webViewModel == nil
       && self.exam_site_id != nil)
    {
        [self reqData];
    }
}


-(void) configNavigationItem
{
    [self.tabBarController configNavigationBarTintColor:UIColorRGBA(0, 0, 0, 1)];
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"midi club详情";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    
    [self configNavigationBackAction:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    //导航栏右边的按钮
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"phone_icon"] style:UIBarButtonItemStyleBordered target:self action:@selector(pressRight:)];
    rightBar.tintColor = UIColorRGBA(118, 207, 201, 1);
    
    [self.navigationItem setRightBarButtonItem:rightBar];
}

#pragma mark QxNetWorkTool delegate
-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if(tag == 1)
    {
        [MBProgressHUD hideHUDForView:self.view];
        
        for(NSDictionary *dic in Response)
        {
            self.detailModel = [MidiClubDetailModel myModelWithDict:dic];
            break;
        }
    }
    else if(tag == 2)
    {
        [MBProgressHUD hideHUDForView:self.view];
        
        for(NSDictionary *dic in Response)
        {
            self.webViewModel = [MidiClubWebViewModel myModelWithDict:dic];
            
            [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webViewModel.url]]];
            break;
        }
    }
}

-(void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    [MBProgressHUD hideHUDForView:self.view];
    [MBProgressHUD showError:(NSString*)Response toView:self.view];
    
}


//请求数据
-(void)reqData
{
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    
    [self.netTool GetDataFromServerWithPostMethodandInterfaceName:MiDiClubDetailInterface andParameter:@[@{@"exam_site_id":self.exam_site_id}] tag:1];
    
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    
    [self.netTool GetDataFromServerWithPostMethodandInterfaceName:GetExamPlaceWebInterface andParameter:@[@{@"site_id":self.exam_site_id}] tag:2];
}

//设置考点id
-(void)setSiteID:(NSString *)siteID
{
    if(siteID == nil)
        return;
    self.exam_site_id = siteID;
}

-(void)pressRight:(UIButton *)sender
{
    [self clickCall:nil];
}

//拨号
-(void)clickCall:(id)sender
{
    if(self.detailModel == nil)
        return;
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:nil
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:self.detailModel.tel,nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    
    [actionSheet showInView:self.view];
    
    actionSheet.tapBlock = ^(UIActionSheet *actionSheet, NSInteger buttonIndex)
    {
        switch (buttonIndex) {
            case 0:
                [self telePhoneCall];
                break;
                
            default:
                break;
        }
    };
}

//打电话
-(void)telePhoneCall
{
//    打电话，有提示框，电话结束返回APP
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.detailModel.tel];
    
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}
@end

















